package com.example.test.login;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.test.R;
import com.example.test.all_Myhelper.MyHelper;


public class register extends AppCompatActivity implements View.OnClickListener{
    MyHelper myHelper;
    public Button register_btn;
    public EditText register_id,register_passwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        myHelper = new MyHelper(this);
        init();
    }
    private void init() {
        register_btn=(Button) findViewById(R.id.register_btn);
        register_id=(EditText) findViewById(R.id.register_id);
        register_passwd=(EditText) findViewById(R.id.register_passwd);
        register_btn.setOnClickListener(this);
        register_id.setOnClickListener(this);
        register_passwd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String id, password;
        SQLiteDatabase db;
        ContentValues values;
        if(v.getId()==R.id.register_btn){
//        switch (v.getId()) {
//            case R.id.register_btn:
//                db = myHelper.getReadableDatabase();
//                Cursor cursor = db.query("user", null, null, null, null, null, null);
//                if (cursor.getCount() == 0) {
//                    System.out.println("没有数据");
//                } else {
//                    System.out.println("用户是" );
//                }
//                db.close();
//                break;
                id = register_id.getText().toString().replaceAll(" ", "");
                password = register_passwd.getText().toString().replaceAll(" ", "");
                db = myHelper.getReadableDatabase();
                values = new ContentValues();
                values.put("name",id);
                values.put("password",password);
                if(id.equals("") && password.equals("")){
                    AlertDialog aldg2;
                    AlertDialog.Builder adbd2 = new AlertDialog.Builder(register.this);
                    adbd2.setTitle("提示");
                    adbd2.setMessage("不允许输入空字符串");
                    adbd2.setNegativeButton("返回", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    aldg2 = adbd2.create();
                    aldg2.show();
                }else{
                    db.insert("user", null, values);
                    AlertDialog aldg;
                    AlertDialog.Builder adbd = new AlertDialog.Builder(register.this);
                    adbd.setTitle("提示");
                    adbd.setMessage("注册成功");
                    adbd.setNegativeButton("返回登录", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(register.this, login_main_activity.class);
                            Bundle bundle = new Bundle();
                            String str_id = register_id.getText().toString();
                            bundle.putString("id", str_id);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                    aldg = adbd.create();
                    aldg.show();
                    System.out.println("信息已添加");
                }
                db.close();
//                break;
        }
    }
}
